trigger TaskBeforeTrigger on Task (before insert,before update) {
    Set<Id> accountIdSet = new Set<Id>();
    Set<Id> leadIdSet = new Set<Id>();
     
     for(Task tk : Trigger.new) {
        String whatIdStr = tk.whatId;
        String whoIdStr = tk.whoId; 
        if(tk.AccountId != null)            
            accountIdSet.add(tk.AccountId);        
        else if (whatIdStr != null && whatIdStr.startswith('001'))   
            accountIdSet.add(tk.whatId);    
        else if(whoIdStr != null && whoIdStr.startswith('00Q'))
            leadIdSet.add(tk.whoId);    
    }
    
    if(accountIdSet.size() > 0) {
   
        Map<Id, Account> accMap = new Map<Id, Account>([Select Id,PersonHomePhone,RecordTypeId, PersonMobilePhone,Name,PersonMailingState,BillingState,recordType.name from Account where Id IN: accountIdSet]);
        Map<Id,RecordType> Map_RecordType = new Map<Id,RecordType>([Select Id, name , DeveloperName from RecordType where sObjectType='Account' And DeveloperName like '%ULA%']);
        for(Task tk : Trigger.new) {
            String whatIdStr = tk.whatId;
            if(tk.AccountId != null)
            {
                tk.State__c = accMap.get(tk.AccountId).PersonMailingState;
                if(trigger.isInsert)
                {
                System.debug('--------- '+Map_RecordType);
                    if(Map_RecordType!= null && tk.AccountId != null && accMap.containsKey(tk.AccountId)!= null && accMap.get(tk.AccountId).RecordTypeId != null && Map_RecordType.containskey(accMap.get(tk.AccountId).RecordTypeId))
                    {
                        tk.Home_Phone__c  = accMap.get(tk.AccountId).PersonHomePhone;
                        tk.Mobile_Phone__c  = accMap.get(tk.AccountId).PersonMobilePhone;         
                    }   
                } 
            }    
            else if (whatIdStr != null && whatIdStr.startswith('001'))  
            {             
                tk.State__c = accMap.get(tk.whatId).PersonMailingState;  
                if(trigger.isInsert)
                {
                    System.debug('----333---- '+ Map_RecordType);                
                    if(Map_RecordType!= null && tk.whatId != null && accMap.containsKey(tk.whatId)!= null && accMap.get(tk.whatId).RecordTypeId != null && Map_RecordType.containskey(accMap.get(tk.whatId).RecordTypeId))
                    {
                        tk.Home_Phone__c  = accMap.get(tk.whatId).PersonHomePhone;
                        tk.Mobile_Phone__c  = accMap.get(tk.whatId).PersonMobilePhone;         
                    } 
                }
            }              
        }    
    }
    
    if(leadIdSet.size() > 0) {
        Map<Id, Lead> leadMap = new Map<Id, Lead>([Select Id, State from Lead where Id IN: leadIdSet]);
        for(Task tk : Trigger.new) {
            String whoIdStr = tk.whoId;
            if(whoIdStr != null && whoIdStr.startswith('00Q'))               
                tk.State__c = leadMap.get(tk.whoId).State;            
        }    
    }
}